﻿using System;
using System.Web.Security;
using System.Web.UI;
using JobSiteStarterKit.BOL;
using JobSiteStarterKit.DAL;

public partial class UserControls_employer_EmployerRegister : System.Web.UI.UserControl
{
    private bool blnRegister;
    public bool IsRegister
    {
        get
        {
            return this.blnRegister;
        }
        set
        {
            this.blnRegister = value;
            RenderMe();
        }
    }

    private void RenderMe()
    {
        if (this.blnRegister == false)
        {
            trAccount.Visible = false;
            trUserName.Visible = false;
            trPassword.Visible = false;
            trEmail.Visible = false;
            ImageButton1.ImageUrl = "~/Images/Company update.png";

        }
        else
        {
            trAccount.Visible = true;
            trUserName.Visible = true;
            trPassword.Visible = true;
            trEmail.Visible = true;
        }
    }

    public void FillEmployerData(string username)
    {
        using (DBAccess db = new DBAccess())
        {
            Employer objemp = new Employer();
            if(Context.User.Identity.IsAuthenticated)
            {
                Employer.EmployerData data = objemp.GetEmployerData(Context.User.Identity.Name);
                txtCity.Text=data.City;
                txtCompanyName.Text=data.CompanyName;
                txtAddress.Text=data.Address;
                txtPhone.Text=data.Phonenumber;
                txtContactPerson.Text=data.ContactPerson;
                txtZip.Text=data.Pincode;
                DrpCpuntry.SelectedValue=data.Country.ToString();
                DrpState.SelectedValue=data.State.ToString();
                if(data.CompanyType !="")
                {
                    if(data.CompanyType == rbtCompany.Text)
                    {
                        rbtCompany.Checked=true;
                    }
                    else{
                        rbtConsultancy.Checked=true;
                    }
                }
                DrpIndustryType.SelectedValue=data.IndustryType;
                txtProfile.Text=data.Profile;
                txtWebsiteUrl.Text=data.WebSite;
                txtUserName.Text=data.UserName;
            }
            

            //string strprocName = "JobsDb_Companies_SelectByUserName";
            //db.CommandText = strprocName;
            //db.AddParameter("@sUsername", username);
            //System.Data.SqlClient.SqlDataReader reader = (System.Data.SqlClient.SqlDataReader)db.ExecuteReader();
            //if (reader.HasRows)
            //{
            //    reader.Read();
            //    txtCity.Text = reader["City"].ToString();

            //    txtCompanyName.Text = reader["CompanyName"].ToString();
            //    txtContactPerson.Text = reader["ContactPerson"].ToString();
            //    txtAddress.Text = reader["Address1"].ToString();
            //    txtPhone.Text = reader["Phone"].ToString();
            //    txtZip.Text = reader["Zip"].ToString();
            //    DrpCpuntry.SelectedValue = reader["CountryID"].ToString();
            //    DrpIndustryType.SelectedValue = reader["IndustryType"].ToString();
            //    DrpState.SelectedValue = reader["StateID"].ToString();
            //    if (reader["CompanyType"].ToString() != "")
            //    {
            //        if (reader["CompanyType"].ToString() == rbtCompany.Text)
            //            rbtCompany.Checked = true;
            //        else
            //            rbtConsultancy.Checked = true;
            //    }

            }
        }

    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            
            DrpCpuntry.DataSource = JobSiteStarterKit.BOL.Country.GetCountries();
            DrpCpuntry.DataTextField = "CountryName";
            DrpCpuntry.DataValueField = "CountryID";
            DrpCpuntry.DataBind();
            DrpCpuntry.SelectedIndex = 76;
            DrpCpuntry.DataBind();

            //filling state IndiaWise
            if (DrpCpuntry.SelectedValue == "76")
            {
                DrpState.DataSource = State.GetStates(76);
                DrpState.DataTextField = "StateName";
                DrpState.DataValueField = "StateID";
                DrpState.DataBind();
            }

            //filling the Industry

            DrpIndustryType.DataSource = Industry.SelectIndustry(); 
            DrpIndustryType.DataTextField = "CategoryName";
            DrpIndustryType.DataValueField = "CategoryID";
            DrpIndustryType.DataBind();

            if (!blnRegister)
            {
                FillEmployerData(Context.User.Identity.Name);
            }
        }
        
    }

    private void RegisterMe()
    {
        string strusername = txtUserName.Text.Trim();
        string strpassword = txtpassword.Text;
        string strEmail = txtEmail.Text.Trim();
        string msg = "";
        string strrolename = System.Web.Configuration.WebConfigurationManager.AppSettings["employerrolename"];
        try
        {

            MembershipUser newUser = Membership.CreateUser(strusername, strpassword, strEmail);
            strusername = newUser.UserName;
            Roles.AddUserToRole(strusername, strrolename);
            JobSiteStarterKit.BOL.Employer.EmployerData data = new JobSiteStarterKit.BOL.Employer.EmployerData();
            JobSiteStarterKit.BOL.Employer objEmp = new JobSiteStarterKit.BOL.Employer();
            data.Address = txtAddress.Text.Trim();
            data.UserName = strusername;
            data.CompanyName = txtCompanyName.Text.Trim();
            data.ContactPerson = txtContactPerson.Text.Trim();
            data.State = Convert.ToInt32(DrpState.SelectedValue);
            data.CompanyType = (rbtCompany.Checked) ? rbtCompany.Text : rbtConsultancy.Text;
            data.EmailId = strEmail;
            data.Phonenumber = txtPhone.Text.Trim();
            data.Pincode = txtZip.Text.Trim();
            data.RegistrationDate = DateTime.Today;
            data.Country = Convert.ToInt32(DrpCpuntry.SelectedItem.Value);
            data.City = txtCity.Text.Trim();
            data.Status = 0;
            data.WebSite = txtWebsiteUrl.Text;
            data.Profile = txtProfile.Text;
            data.IndustryType = DrpIndustryType.SelectedValue;
            //data.IndustryType =

            JobSiteStarterKit.DAL.DBAccess db = new JobSiteStarterKit.DAL.DBAccess();

            objEmp.EmployerDetails = data;

            if (objEmp.RegisterEmployer())
            {
                // FormsAuthentication.SetAuthCookie(strusername, false);
                //send email  to the user with registration url so that his/her email
                //will verified.
                //write code to send email

                Common.SendEMAILVerificationMail(strEmail, strusername, "1");


            }
            Response.Redirect("success.aspx");

        }
        catch (MembershipCreateUserException exU)
        {
            msg = Common.CreateUserError(exU.StatusCode);
            lblError.Text = msg;
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            // try to delete the current user and roles from the database if any sqlserver exception occurrs.
            lblError.Text = ex.Message;

        }
       
    }

    private void UpdateMe()
    {
        JobSiteStarterKit.BOL.Employer.EmployerData data;
        JobSiteStarterKit.BOL.Employer objEmp = new JobSiteStarterKit.BOL.Employer();
        data = objEmp.EmployerDetails;
        data.Address = txtAddress.Text;
        data.City = txtCity.Text;
        data.CompanyName = txtCompanyName.Text;
        data.CompanyType = (rbtCompany.Checked)? rbtCompany.Text:rbtConsultancy.Text;
        data.ContactPerson = txtContactPerson.Text;
        data.Country =Convert.ToInt32( DrpCpuntry.SelectedValue);
        data.EmailId = txtEmail.Text;
        data.IndustryType = DrpIndustryType.SelectedValue;
        data.Phonenumber = txtPhone.Text;
        data.Pincode = txtZip.Text;
        data.Profile = txtProfile.Text;
        data.State = Convert.ToInt32(DrpState.SelectedValue);
        data.UserName = Context.User.Identity.Name;
        data.WebSite = txtWebsiteUrl.Text;
        objEmp.EmployerDetails = data;
        if (objEmp.UpdateEmployer(Context.User.Identity.Name))
        {
            lblError.Text = "Your data saved successfully";
        }
        else
        {
            lblError.Text = "Data can't save!!! Please try again...";
        }
        
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (this.blnRegister)
        {
            this.RegisterMe();
        }
        else
        {
            UpdateMe();
        }
    
      
        
    }

    void Membership_ValidatingPassword(object sender, ValidatePasswordEventArgs e)
    {
        //throw new NotImplementedException();
        e.Cancel = false;
       
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
       
    }
    protected void DrpIndustryType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DrpIndustryType.SelectedItem.Text.ToLower() == "others")
            txtindustryOthers.Visible = true;
        else
            txtindustryOthers.Visible = false;
    }

    protected void DrpCpuntry_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Convert.ToInt32(DrpCpuntry.SelectedItem.Value) != -1)
        {
            DrpState.DataSource = JobSiteStarterKit.BOL.State.GetStates(Convert.ToInt32(DrpCpuntry.SelectedItem.Value));
            DrpState.DataTextField = "StateName";
            DrpState.DataValueField = "StateId";
            DrpState.DataBind();
        }
    }
    protected void btnCheckUser_Click(object sender, EventArgs e)
    {
        MembershipUserCollection obj =  Membership.FindUsersByName(txtUserName.Text.Trim());
        if (obj.Count > 0)
        {
            lblError.Text = "User Name ( " + txtUserName.Text.Trim() + " ) is already taken. <br /> Please try another username.";
        }

    }
}
